package net.zhengxinyun.performance.controller.index;

import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.UserEntity;
import net.zhengxinyun.performance.filter.*;
import net.zhengxinyun.performance.service.Check.CheckService;
import net.zhengxinyun.performance.service.incentive.IncentiveService;
import net.zhengxinyun.performance.service.indicators.*;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.measurement.AssessmentTestService;
import net.zhengxinyun.performance.service.news.NewsService;
import net.zhengxinyun.performance.service.workSupervision.WorkScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hyp1202 on 2019/1/28.
 */
@RestController
@RequestMapping("/index")
public class IndexController {

    private CountryIndicatorsService countryIndicatorsService;
    private CountryScoreService countryScoreService;
    private SixIndicatorsService sixIndicatorsService;
    private SixScoreService sixScoreService;
    private TownIndicatorsService townIndicatorsService;
    private TownScoreService townScoreService;
    private AssessmentTestService assessmentTestService;
    private TokenService tokenService;
    private IncentiveService incentiveService;
    private WorkScoreService workScoreService;
    private CheckService checkService;
    private NewsService newsService;
    private LoginService loginService;

    @Autowired
    public IndexController(CountryIndicatorsService countryIndicatorsService, CountryScoreService countryScoreService, SixIndicatorsService sixIndicatorsService, SixScoreService sixScoreService, TownIndicatorsService townIndicatorsService, TownScoreService townScoreService, AssessmentTestService assessmentTestService, TokenService tokenService, IncentiveService incentiveService, WorkScoreService workScoreService, CheckService checkService, NewsService newsService, LoginService loginService) {
        this.countryIndicatorsService = countryIndicatorsService;
        this.countryScoreService = countryScoreService;
        this.sixIndicatorsService = sixIndicatorsService;
        this.sixScoreService = sixScoreService;
        this.townIndicatorsService = townIndicatorsService;
        this.townScoreService = townScoreService;
        this.assessmentTestService = assessmentTestService;
        this.tokenService = tokenService;
        this.incentiveService = incentiveService;
        this.workScoreService = workScoreService;
        this.checkService = checkService;
        this.newsService = newsService;
        this.loginService = loginService;
    }

    @PostMapping("/indexCountryIndicators")
    public Result<ArrayList<CountryIndicatorsFilter>> indexCountryIndicators(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            ArrayList<CountryIndicatorsFilter> list ;
//            if ("1".equals(param.indexFilter.getType())){
                list = (ArrayList<CountryIndicatorsFilter>) countryScoreService.queryFirstIndicatorsScore(param.indexFilter);
//            }else {
//                list = (ArrayList<CountryIndicatorsFilter>) countryScoreService.queryFirstIndicatorsScoreReverse(param.indexFilter);
//            }
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/indexMeasurement")
    public Result<ArrayList<MeasurementInformationFilter>> indexMeasurement(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            ArrayList<MeasurementInformationFilter> list = (ArrayList<MeasurementInformationFilter>) assessmentTestService.indexMeasurement();
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/indexIncentive")
    public Result<HashMap<String,Object>> indexIncentive(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = incentiveService.indexIncentive(param.indexFilter);
            return ResultUtils.getResults(Result.Status.SUCCESS,"",map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/indexWork")
    public Result<ArrayList<WorkScoreFilter>> indexWork(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            ArrayList<WorkScoreFilter> list = (ArrayList<WorkScoreFilter>) workScoreService.indexWork(param.indexFilter);
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/indexTown")
    public Result<HashMap<String,Object>> indexTown(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            TownIndicatorsFilter townIndicatorsFilter = new TownIndicatorsFilter();
            townIndicatorsFilter.setIndexType("一级指标");
            if (StringUtils.isBlank(param.indexFilter.getDateTime())){
                param.indexFilter.setDateTime(DateUtils.getYear());
            }else {
                townIndicatorsFilter.setDateTime(param.indexFilter.getDateTime());
            }
            List<TownIndicatorsFilter> firstIndexList = townIndicatorsService.query(townIndicatorsFilter);
            List<Map<String,Object>> indexList = townIndicatorsService.getIndexMap(firstIndexList);
            map.put("indexMap",indexList);
            if (StringUtils.isBlank(param.indexFilter.getIndexName())){
                String indexName = "";
                ArrayList<TownIndicatorsFilter> indicatorsFilterArrayList = (ArrayList<TownIndicatorsFilter>) townIndicatorsService.indexTown(param.indexFilter);
                if (indicatorsFilterArrayList != null && indicatorsFilterArrayList.size() != 0){
                    indexName = indicatorsFilterArrayList.get(0).getIndexName();
                }
                param.indexFilter.setIndexName(indexName);
                ArrayList<TownIndicatorsFilter> list = (ArrayList<TownIndicatorsFilter>) townIndicatorsService.indexTown(param.indexFilter);
                map.put("list",list);
            }else {
                ArrayList<TownIndicatorsFilter> list = (ArrayList<TownIndicatorsFilter>) townIndicatorsService.indexTown(param.indexFilter);
                map.put("list",list);
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"",map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/indexSix")
    public Result<HashMap<String,Object>> indexSix(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            SixIndicatorsFilter sixIndicatorsFilter = new SixIndicatorsFilter();
            sixIndicatorsFilter.setIndexType("一级指标");
            if (StringUtils.isBlank(param.indexFilter.getDateTime())){
                param.indexFilter.setDateTime(DateUtils.getYear());
            }else {
                sixIndicatorsFilter.setDateTime(param.indexFilter.getDateTime());
            }
            List<SixIndicatorsFilter> firstIndexList = sixIndicatorsService.query(sixIndicatorsFilter);
            List<Map<String,Object>> indexList = sixIndicatorsService.getIndexMap(firstIndexList);
            map.put("indexMap",indexList);
            if (StringUtils.isBlank(param.indexFilter.getIndexName())){
                String indexName = "";
                ArrayList<SixIndicatorsFilter> indicatorsFilterArrayList = (ArrayList<SixIndicatorsFilter>) sixIndicatorsService.indexTown(param.indexFilter);
                if (indicatorsFilterArrayList != null && indicatorsFilterArrayList.size() != 0){
                    indexName = indicatorsFilterArrayList.get(0).getIndexName();
                }
                param.indexFilter.setIndexName(indexName);
                ArrayList<SixIndicatorsFilter> list = (ArrayList<SixIndicatorsFilter>) sixIndicatorsService.indexTown(param.indexFilter);
                map.put("list",list);
            }else {
                ArrayList<SixIndicatorsFilter> list = (ArrayList<SixIndicatorsFilter>) sixIndicatorsService.indexTown(param.indexFilter);
                map.put("list",list);
            }
            return ResultUtils.getResults(Result.Status.SUCCESS,"",map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @RequestMapping("/indexTop")
    public Result<HashMap<String,Object>> indexTop(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            HashMap<String,Object> map = new HashMap<>();
            UserEntity userEntity = loginService.getUserByToken(param.tokenEntity);
            MeasurementInformationFilter measurementInformationFilter = assessmentTestService.indexSumScore(userEntity);
            Double incentive = incentiveService.indexForScore(param.indexFilter);
            Double work = workScoreService.queryForSum(param.indexFilter);
            List<CheckFilter> list = checkService.querySumPoint(param.indexFilter);
            Double countryScore = countryScoreService.queryForSumScore(param.indexFilter);
            map.put("countryScore",countryScore);
            map.put("incentive",incentive);
            map.put("work",work);
            map.put("check",list);
            map.put("measurement",measurementInformationFilter);
            return ResultUtils.getResults(Result.Status.SUCCESS,"",map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/news")
    public Result<ArrayList<NewsFilter>> shuffling(@RequestBody IndexParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            ArrayList<NewsFilter> list = (ArrayList<NewsFilter>) newsService.shuffling();
            return ResultUtils.getSuccessResults(list);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }


}
class IndexParam{
    public IndexFilter indexFilter;
    public TokenEntity tokenEntity;
}
